package com.maxd.service.helpTask;

import com.maxd.model.*;
import com.maxd.respository.helpTask.HelpGetTaskJpaRepository;
import com.maxd.respository.helpTask.HelpGetTaskRepository;
import com.maxd.respository.helpTask.HelpTaskPlatformRepository;
import com.maxd.respository.userinfo.UserJpaRepository;
import com.maxd.service.lucky.ILuckyQueueRecordService;
import com.maxd.service.lucky.ILuckyQueueService;
import com.maxd.service.lucky.ILuckyValueQueueService;
import com.maxd.service.message.IMessageService;
import com.maxd.service.userinfo.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HelpGetTaskServiceImpl implements IHelpGetTaskeSrvice {

    private final HelpGetTaskJpaRepository helpGetTaskJpaRepository;
    private final HelpGetTaskRepository helpGetTaskRepository;
    private final HelpTaskPlatformRepository helpTaskPlatformRepository;
    private final IHelpLuckyValueService iHelpLuckyValueService;
    private final UserJpaRepository userJpaRepository;
    private final IUserService userService;
    private final IMessageService iMessageService;
    private final ILuckyQueueService luckyQueueService;
    private final IHelpLuckyValueService luckyValueService;
    private final ILuckyValueQueueService luckyValueQueueService;
    private final ILuckyQueueRecordService iLuckyQueueRecordService;
    private final IHelpLuckyDetailsService iHelpLuckyDetailsService;
    private final ILuckyValueQueueService iLuckyValueQueueService;

    @Autowired
    public HelpGetTaskServiceImpl(HelpGetTaskJpaRepository helpGetTaskJpaRepository, HelpGetTaskRepository helpGetTaskRepository, HelpTaskPlatformRepository helpTaskPlatformRepository, IHelpLuckyValueService iHelpLuckyValueService, UserJpaRepository userJpaRepository, IUserService userService, IMessageService iMessageService, ILuckyQueueService luckyQueueService, IHelpLuckyValueService luckyValueService, ILuckyValueQueueService luckyValueQueueService, ILuckyQueueRecordService iLuckyQueueRecordService, IHelpLuckyDetailsService iHelpLuckyDetailsService, ILuckyValueQueueService iLuckyValueQueueService) {
        this.helpGetTaskJpaRepository = helpGetTaskJpaRepository;
        this.helpGetTaskRepository = helpGetTaskRepository;
        this.helpTaskPlatformRepository = helpTaskPlatformRepository;
        this.iHelpLuckyValueService = iHelpLuckyValueService;
        this.userJpaRepository = userJpaRepository;
        this.userService = userService;
        this.iMessageService = iMessageService;
        this.luckyQueueService = luckyQueueService;
        this.luckyValueService = luckyValueService;
        this.luckyValueQueueService = luckyValueQueueService;
        this.iLuckyQueueRecordService = iLuckyQueueRecordService;
        this.iHelpLuckyDetailsService = iHelpLuckyDetailsService;
        this.iLuckyValueQueueService = iLuckyValueQueueService;
    }

    @Override
    public HelpGetTask saveBody(Long helpTaskPlatformId,Long userId){
        HelpGetTask helpGetTask=new HelpGetTask();
        helpGetTask.setHelpTaskPlatformId(helpTaskPlatformId);
        helpGetTask.setUserId(userId);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        helpGetTask.setState(0);
        helpGetTask.setCreateTime(sdf.format(new Date()));
        return helpGetTaskJpaRepository.save(helpGetTask);
    }

    @Override
    public HelpGetTask updateHelpGetTaskById(HelpGetTask helpGetTask){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        helpGetTask.setCreateTime(sdf.format(new Date()));
        return helpGetTaskJpaRepository.save(helpGetTask);
    }

    @Override
    public Page<Map<String, Object>> selectUnderwayHelpGetTaskList(Pageable pageable) {
        return helpGetTaskRepository.selectUnderwayHelpGetTaskList(pageable);
    }

    @Override
    public void deleteHelpGetTaskById(Long id){
        helpGetTaskJpaRepository.deleteById(id);
    }

    @Override
    public void deleteHelpGetTaskByIds(String ids){
        for(String id:ids.split(",")){
            helpGetTaskJpaRepository.deleteById(Long.parseLong(id));
        }
    }

    @Override
    public Page<Map<String,Object>> selectHelpGetTaskList(Pageable pageable, Long userId,Integer state){
        if(state==null || state==-1){
            return helpGetTaskRepository.selectHelpGetTaskList(pageable,userId);
        }
        return helpGetTaskRepository.selectHelpGetTaskList(pageable,userId,state);
    }

    @Override
    public Map<String,Object> selectHelpTaskPlatform(Long id){
        return helpTaskPlatformRepository.selectById(id);
    }

    @Override
    public Map<String,Object> selectHelpGetTaskById(Long id,Long userId){
        Integer count = helpGetTaskRepository.selectCount(id, userId);
        Map<String, Object> map=new HashMap<>();
        if(count==null || count==0){
            Map<String, Object> map1 = helpTaskPlatformRepository.selectById(id);
            map.put("flag",1);
            map.put("data",map1);
            return map;
        }
        Map<String, Object> map1 = helpGetTaskRepository.selectHelpGetTaskById(id, userId);
        map.put("flag",2);
        map.put("data",map1);
        return map;
    }

    @Override
    public Page<Map<String,Object>> selectHelpGetTaskList(Pageable pageable){
        return helpGetTaskRepository.selectHelpGetTaskList(pageable);
    }

    @Override
    public int  helpGetTaskRemind(Long id){
        HelpGetTask helpGetTask = selectById(id);
        Map<String, Object> map = helpTaskPlatformRepository.selectById(helpGetTask.getHelpTaskPlatformId());
        UserInfo userByWxId = userJpaRepository.findById(helpGetTask.getUserId()).orElse(null);
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setContent(userByWxId.getNickName() + " 您好！您的接单已经很长时间没有去完成了，平台温馨提示您请尽快完成任务，超时会自动取消哦！任务标题:"+map.get("title"));
        if (userByWxId.getClientid() != null) {
            userService.pushToSingle("任务提醒", userByWxId.getNickName() + " 您好！您的接单已经很长时间没有去完成了，平台温馨提示您请尽快完成任务，超时会自动取消哦！任务标题:"+map.get("title"), userByWxId.getClientid());
        }
        messageInfo.setState(String.valueOf(6));
        messageInfo.setTitle("任务提醒");
        messageInfo.setUserName(userByWxId.getNickName());
        messageInfo.setUserId(String.valueOf(userByWxId.getId()));
        iMessageService.saveBody(messageInfo);
        return 1;
    }

    @Override
    public int exitHelpGetTask(Long id){
        HelpGetTask helpGetTask = selectById(id);
        helpGetTask.setState(4);
        updateHelpGetTaskById(helpGetTask);
        Map<String, Object> map = helpTaskPlatformRepository.selectById(helpGetTask.getHelpTaskPlatformId());
        UserInfo userByWxId = userJpaRepository.findById(helpGetTask.getUserId()).orElse(null);
        if(userByWxId!=null){
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setContent(userByWxId.getNickName() + " 您好！您的接单已经很长时间没有去完成了，平台已经自动帮您取消了！任务标题:"+map.get("title"));
            if (userByWxId.getClientid() != null) {
                userService.pushToSingle("任务超时", userByWxId.getNickName() + " 您好！您的接单已经很长时间没有去完成了，平台已经自动帮您取消了！任务标题:"+map.get("title"), userByWxId.getClientid());
            }
            messageInfo.setState(String.valueOf(6));
            messageInfo.setTitle("任务超时");
            messageInfo.setUserName(userByWxId.getNickName());
            messageInfo.setUserId(String.valueOf(userByWxId.getId()));
            iMessageService.saveBody(messageInfo);
        }
        return 1;
    }



    @Override
    @Transactional
    public int auditHelpGetTask(String ids,Integer state,String auditContent){
        for(String id:ids.split(",")){
            Long helpGetTaskId=Long.parseLong(id);
            HelpGetTask helpGetTask = helpGetTaskRepository.selectById(helpGetTaskId);
            MessageInfo messageInfo = new MessageInfo();
            if(helpGetTask.getState()==1){
                UserInfo userByWxId = userJpaRepository.findById(helpGetTask.getUserId()).orElse(null);
                if(state==1){//同意
                    helpGetTaskRepository.updateHelpGetTaskState(helpGetTaskId,2,"审核成功");
                    Map<String, Object> stringObjectMap = helpTaskPlatformRepository.selectById(helpGetTask.getHelpTaskPlatformId());
                    iHelpLuckyValueService.updateHelpLuckyValue(helpGetTask.getUserId(),Double.parseDouble(stringObjectMap.get("luckyValue").toString()));
                    messageInfo.setContent(userByWxId.getNickName() + " 您好！您的接单任务已通过！幸运值增加："+stringObjectMap.get("luckyValue").toString());
                    if (userByWxId.getClientid() != null) {
                        userService.pushToSingle("接单任务", userByWxId.getNickName() + " 您好！您的接单任务已通过！", userByWxId.getClientid());
                    }
                    //幸运值详细信息
                    HelpLuckyDetails helpLuckyDetails=new HelpLuckyDetails();
                    helpLuckyDetails.setUserId(userByWxId.getId());
                    helpLuckyDetails.setHelpTaskId(helpGetTask.getHelpTaskPlatformId());
                    helpLuckyDetails.setTitle("接单任务");
                    helpLuckyDetails.setContent(userByWxId.getNickName() + " 您好！您的接单任务已通过！幸运值增加："+stringObjectMap.get("luckyValue").toString());
                    helpLuckyDetails.setLuckyValue(Double.parseDouble(stringObjectMap.get("luckyValue").toString()));
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    helpLuckyDetails.setCreateTime(sdf.format(new Date()));
                    iHelpLuckyDetailsService.saveBody(helpLuckyDetails);
                    updateLucky(userByWxId);
                }else{//拒绝
                    helpGetTaskRepository.updateHelpGetTaskState(helpGetTaskId,3,auditContent);
                    messageInfo.setContent(userByWxId.getNickName() + " 您好！您的接单任务被拒绝！拒绝原因："+auditContent);
                    if (userByWxId.getClientid() != null) {
                        userService.pushToSingle("接单任务", userByWxId.getNickName() + " 您好！您的接单任务被拒绝！拒绝原因："+auditContent, userByWxId.getClientid());
                    }
                }
                messageInfo.setState(String.valueOf(6));
                messageInfo.setTitle("接单任务通知！");
                messageInfo.setUserName(userByWxId.getNickName());
                messageInfo.setUserId(String.valueOf(userByWxId.getId()));
                iMessageService.saveBody(messageInfo);
            }
        }
        return 1;
    }

    /**
     * 计算幸运值队列是否晋级  判断条件必须同时满足
     * 1 积分达到
     * 2 邀请的人必须成为同等或以上会员
     * 3 队列在1-13之间 必须满足13个人 之后9的倍数 必须满足
     */
    @Override
    public int updateLucky(UserInfo userByWxId){
        MessageInfo messageInfo = new MessageInfo();
        //先判断是否已经发起过申请
        LuckyQueueRecord oldLuckyQueueRecord = iLuckyQueueRecordService.selectByUserId(userByWxId.getId());
        if(oldLuckyQueueRecord==null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            LuckyValueQueue luckyValueQueue = luckyValueQueueService.selectLuckyValueQueueByUserId(userByWxId.getId());
            HelpLuckyValue helpLuckyValue = luckyValueService.selectByUserId(userByWxId.getId());
            //获取平台积分晋级积分条件 判断当前积分是否满足 积分晋级条件
            List<LuckyQueue> luckyQueues = luckyQueueService.selectLuckyQueueListAsc();
            //判断是否已经达到最高等级
            if((luckyValueQueue==null)||(!luckyValueQueue.getLuckyQueueId().equals(luckyQueues.get(luckyQueues.size()-1).getId()))) {
                for(int j=0;j<luckyQueues.size();j++){
                    if (helpLuckyValue.getLuckyValue() >= luckyQueues.get(j).getLuckyValue()) {
                        //如果用户所在队列为空  则必须是第一队列
                        if (luckyValueQueue == null) {
                            //第一队列条件满足  提交晋级申请
                            LuckyQueueRecord luckyQueueRecord = new LuckyQueueRecord();
                            luckyQueueRecord.setLuckyQueueId(luckyQueues.get(0).getId());
                            luckyQueueRecord.setUserId(userByWxId.getId());
                            luckyQueueRecord.setCreateTime(sdf.format(new Date()));
                            luckyQueueRecord.setState(0);
                            iLuckyQueueRecordService.saveBody(luckyQueueRecord);
                            messageInfo.setContent(userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(0).getName());
                            if (userByWxId.getClientid() != null) {
                                userService.pushToSingle("队列晋级通知", userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(0).getName(), userByWxId.getClientid());
                            }
                            messageInfo.setState(String.valueOf(6));
                            messageInfo.setTitle("队列晋级通知！");
                            messageInfo.setUserName(userByWxId.getNickName());
                            messageInfo.setUserId(String.valueOf(userByWxId.getId()));
                            iMessageService.saveBody(messageInfo);
                            return 1;
                        } else {
                            //除晋级第一队列外 其他队列晋级需要满足条件方可
                            if(luckyQueues.get(j).getSort()!=1){
                                LuckyQueue luckyQueue1 = luckyQueueService.selectLuckyQueueById(luckyValueQueue.getLuckyQueueId());
                                //用户所在队列必须等于前一个队列 才可晋级
                                if (luckyQueue1.getSort() .equals( luckyQueues.get(j-1).getSort())) {
                                    //判断邀请的好友是否满足条件  邀请的好友是否达到当前级别或者级别以上
                                    Integer integer = luckyValueQueueService.selectUserLuckyQueueByInvitation(userByWxId.getRelationId(), luckyQueues.get(j-1).getSort());
                                    //邀请的好友必须大于等于自己当前等级晋级所需人数
                                    if (integer != null && integer >= luckyQueue1.getNumberOfPeople()) {
                                        //判断自己所在当前队列的位置
                                        Integer integer1 = luckyValueQueueService.selectLuckQueueLocationByUserId(userByWxId.getId(), luckyValueQueue.getLuckyQueueId());
                                        //当前队列总人数
                                        Integer integer2 = luckyValueQueueService.selectLuckQueueCount(luckyValueQueue.getLuckyQueueId());
                                        //队列晋级条件 前13名必须凑够13人方可晋级  后面按照9人一队为顺序 方可晋级
                                        if (integer1 != null && integer2 != null) {
                                            if (integer1 <= luckyQueue1.getStartPerson()) {
                                                //队列人数大于13人 提交晋级申请
                                                if (integer2 >= luckyQueue1.getStartPerson()) {
                                                    //条件满足 提交晋级申请
                                                    LuckyQueueRecord luckyQueueRecord = new LuckyQueueRecord();
                                                    luckyQueueRecord.setLuckyQueueId(luckyQueues.get(j).getId());
                                                    luckyQueueRecord.setUserId(userByWxId.getId());
                                                    luckyQueueRecord.setCreateTime(sdf.format(new Date()));
                                                    luckyQueueRecord.setState(0);
                                                    iLuckyQueueRecordService.saveBody(luckyQueueRecord);
                                                    luckyValueQueueService.updateLuckyValueQueue(2,luckyValueQueue.getUserId());
                                                    messageInfo.setContent(userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName());
                                                    if (userByWxId.getClientid() != null) {
                                                        userService.pushToSingle("队列晋级通知", userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName(), userByWxId.getClientid());
                                                    }
                                                    messageInfo.setState(String.valueOf(6));
                                                    messageInfo.setTitle("队列晋级通知！");
                                                    messageInfo.setUserName(userByWxId.getNickName());
                                                    messageInfo.setUserId(String.valueOf(userByWxId.getId()));
                                                    iMessageService.saveBody(messageInfo);
                                                    //有人晋级后 需要将队列重新刷一遍
                                                    List<Long> longs = iLuckyValueQueueService.selectLuckyQueueUserIdByLuckyQueueId(luckyQueues.get(j-1).getId());
                                                    for(Long userId1:longs){
                                                        UserInfo userInfo1 = userJpaRepository.findById(userId1).orElse(null);
                                                        if(userInfo1!=null){
                                                            updateLucky1(userInfo1);
                                                        }
                                                    }
                                                    return 1;
                                                }
                                            } else {
                                                //根据当前所在位置 进行计算 必须满足9人方可晋级
                                                integer1 = integer1 - luckyQueue1.getStartPerson();
                                                integer2 = integer2 - luckyQueue1.getStartPerson();
                                                //根据当前位置和9的倍数 获取所在队列
                                                int i = integer1 / luckyQueue1.getIntervalPerson();
                                                //判断是否能够除尽
                                                int count;
                                                if (integer1 % luckyQueue1.getIntervalPerson() == 0) {
                                                    count = i * luckyQueue1.getIntervalPerson();
                                                } else {
                                                    count = (i * luckyQueue1.getIntervalPerson()) + luckyQueue1.getIntervalPerson();
                                                }
                                                //判断自己所在队列人数是否满足条件
                                                if (integer2 >= count) {
                                                    //条件满足 提交晋级申请
                                                    LuckyQueueRecord luckyQueueRecord = new LuckyQueueRecord();
                                                    luckyQueueRecord.setLuckyQueueId(luckyQueues.get(j).getId());
                                                    luckyQueueRecord.setUserId(userByWxId.getId());
                                                    luckyQueueRecord.setCreateTime(sdf.format(new Date()));
                                                    luckyQueueRecord.setState(0);
                                                    iLuckyQueueRecordService.saveBody(luckyQueueRecord);
                                                    luckyValueQueueService.updateLuckyValueQueue(2,luckyValueQueue.getUserId());
                                                    messageInfo.setContent(userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName());
                                                    if (userByWxId.getClientid() != null) {
                                                        userService.pushToSingle("队列晋级通知", userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName(), userByWxId.getClientid());
                                                    }
                                                    messageInfo.setState(String.valueOf(6));
                                                    messageInfo.setTitle("队列晋级通知！");
                                                    messageInfo.setUserName(userByWxId.getNickName());
                                                    messageInfo.setUserId(String.valueOf(userByWxId.getId()));
                                                    iMessageService.saveBody(messageInfo);
                                                    //有人晋级后 需要将队列重新刷一遍
                                                    List<Long> longs = iLuckyValueQueueService.selectLuckyQueueUserIdByLuckyQueueId(luckyQueues.get(j-1).getId());
                                                    for(Long userId1:longs){
                                                        UserInfo userInfo1 = userJpaRepository.findById(userId1).orElse(null);
                                                        if(userInfo1!=null){
                                                            updateLucky1(userInfo1);
                                                        }
                                                    }
                                                    return 1;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }


    private int updateLucky1(UserInfo userByWxId){
        MessageInfo messageInfo = new MessageInfo();
        //先判断是否已经发起过申请
        LuckyQueueRecord oldLuckyQueueRecord = iLuckyQueueRecordService.selectByUserId(userByWxId.getId());
        if(oldLuckyQueueRecord==null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            LuckyValueQueue luckyValueQueue = luckyValueQueueService.selectLuckyValueQueueByUserId(userByWxId.getId());
            HelpLuckyValue helpLuckyValue = luckyValueService.selectByUserId(userByWxId.getId());
            //获取平台积分晋级积分条件 判断当前积分是否满足 积分晋级条件
            List<LuckyQueue> luckyQueues = luckyQueueService.selectLuckyQueueListAsc();
            //判断是否已经达到最高等级
            if((luckyValueQueue==null)||(!luckyValueQueue.getLuckyQueueId().equals(luckyQueues.get(luckyQueues.size()-1).getId()))) {
                for(int j=0;j<luckyQueues.size();j++){
                    if (helpLuckyValue.getLuckyValue() >= luckyQueues.get(j).getLuckyValue()) {
                        //如果用户所在队列为空  则必须是第一队列
                        if (luckyValueQueue == null) {
                            //第一队列条件满足  提交晋级申请
                            LuckyQueueRecord luckyQueueRecord = new LuckyQueueRecord();
                            luckyQueueRecord.setLuckyQueueId(luckyQueues.get(0).getId());
                            luckyQueueRecord.setUserId(userByWxId.getId());
                            luckyQueueRecord.setCreateTime(sdf.format(new Date()));
                            luckyQueueRecord.setState(0);
                            iLuckyQueueRecordService.saveBody(luckyQueueRecord);
                            messageInfo.setContent(userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(0).getName());
                            if (userByWxId.getClientid() != null) {
                                userService.pushToSingle("队列晋级通知", userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(0).getName(), userByWxId.getClientid());
                            }
                            messageInfo.setState(String.valueOf(6));
                            messageInfo.setTitle("队列晋级通知！");
                            messageInfo.setUserName(userByWxId.getNickName());
                            messageInfo.setUserId(String.valueOf(userByWxId.getId()));
                            iMessageService.saveBody(messageInfo);
                            return 1;
                        } else {
                            //除晋级第一队列外 其他队列晋级需要满足条件方可
                            if(luckyQueues.get(j).getSort()!=1){
                                LuckyQueue luckyQueue1 = luckyQueueService.selectLuckyQueueById(luckyValueQueue.getLuckyQueueId());
                                //用户所在队列必须等于前一个队列 才可晋级
                                if (luckyQueue1.getSort() .equals( luckyQueues.get(j-1).getSort())) {
                                    //判断邀请的好友是否满足条件  邀请的好友是否达到当前级别或者级别以上
                                    Integer integer = luckyValueQueueService.selectUserLuckyQueueByInvitation(userByWxId.getRelationId(), luckyQueues.get(j-1).getSort());
                                    //邀请的好友必须大于等于自己当前等级晋级所需人数
                                    if (integer != null && integer >= luckyQueue1.getNumberOfPeople()) {
                                        //判断自己所在当前队列的位置
                                        Integer integer1 = luckyValueQueueService.selectLuckQueueLocationByUserId(userByWxId.getId(), luckyValueQueue.getLuckyQueueId());
                                        //当前队列总人数
                                        Integer integer2 = luckyValueQueueService.selectLuckQueueCount(luckyValueQueue.getLuckyQueueId());
                                        //队列晋级条件 前13名必须凑够13人方可晋级  后面按照9人一队为顺序 方可晋级
                                        if (integer1 != null && integer2 != null) {
                                            if (integer1 <= luckyQueue1.getStartPerson()) {
                                                //队列人数大于13人 提交晋级申请
                                                if (integer2 >= luckyQueue1.getStartPerson()) {
                                                    //条件满足 提交晋级申请
                                                    LuckyQueueRecord luckyQueueRecord = new LuckyQueueRecord();
                                                    luckyQueueRecord.setLuckyQueueId(luckyQueues.get(j).getId());
                                                    luckyQueueRecord.setUserId(userByWxId.getId());
                                                    luckyQueueRecord.setCreateTime(sdf.format(new Date()));
                                                    luckyQueueRecord.setState(0);
                                                    iLuckyQueueRecordService.saveBody(luckyQueueRecord);
                                                    luckyValueQueueService.updateLuckyValueQueue(2,luckyValueQueue.getUserId());
                                                    messageInfo.setContent(userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName());
                                                    if (userByWxId.getClientid() != null) {
                                                        userService.pushToSingle("队列晋级通知", userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName(), userByWxId.getClientid());
                                                    }
                                                    messageInfo.setState(String.valueOf(6));
                                                    messageInfo.setTitle("队列晋级通知！");
                                                    messageInfo.setUserName(userByWxId.getNickName());
                                                    messageInfo.setUserId(String.valueOf(userByWxId.getId()));
                                                    iMessageService.saveBody(messageInfo);
                                                    return 1;
                                                }
                                            } else {
                                                //根据当前所在位置 进行计算 必须满足9人方可晋级
                                                integer1 = integer1 - luckyQueue1.getStartPerson();
                                                integer2 = integer2 - luckyQueue1.getStartPerson();
                                                //根据当前位置和9的倍数 获取所在队列
                                                int i = integer1 / luckyQueue1.getIntervalPerson();
                                                //判断是否能够除尽
                                                int count;
                                                if (integer1 % luckyQueue1.getIntervalPerson() == 0) {
                                                    count = i * luckyQueue1.getIntervalPerson();
                                                } else {
                                                    count = (i * luckyQueue1.getIntervalPerson()) + luckyQueue1.getIntervalPerson();
                                                }
                                                //判断自己所在队列人数是否满足条件
                                                if (integer2 >= count) {
                                                    //条件满足 提交晋级申请
                                                    LuckyQueueRecord luckyQueueRecord = new LuckyQueueRecord();
                                                    luckyQueueRecord.setLuckyQueueId(luckyQueues.get(j).getId());
                                                    luckyQueueRecord.setUserId(userByWxId.getId());
                                                    luckyQueueRecord.setCreateTime(sdf.format(new Date()));
                                                    luckyQueueRecord.setState(0);
                                                    iLuckyQueueRecordService.saveBody(luckyQueueRecord);
                                                    luckyValueQueueService.updateLuckyValueQueue(2,luckyValueQueue.getUserId());
                                                    messageInfo.setContent(userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName());
                                                    if (userByWxId.getClientid() != null) {
                                                        userService.pushToSingle("队列晋级通知", userByWxId.getNickName() + " 您好！您的红包榜晋级条件已满足！自动帮您申请晋级新的队列:"+luckyQueues.get(j).getName(), userByWxId.getClientid());
                                                    }
                                                    messageInfo.setState(String.valueOf(6));
                                                    messageInfo.setTitle("队列晋级通知！");
                                                    messageInfo.setUserName(userByWxId.getNickName());
                                                    messageInfo.setUserId(String.valueOf(userByWxId.getId()));
                                                    iMessageService.saveBody(messageInfo);
                                                    return 1;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }




    @Override
    public int updateLuckys(UserInfo userByWxId){
        //先判断是否已经发起过申请
        LuckyQueueRecord luckyQueueRecord = iLuckyQueueRecordService.selectByUserId(userByWxId.getId());
        if(luckyQueueRecord!=null){
            return 0;
        }
        LuckyValueQueue luckyValueQueue = luckyValueQueueService.selectLuckyValueQueueByUserId(userByWxId.getId());
        HelpLuckyValue helpLuckyValue = luckyValueService.selectByUserId(userByWxId.getId());
        //获取平台积分晋级积分条件 判断当前积分是否满足 积分晋级条件
        List<LuckyQueue> luckyQueues = luckyQueueService.selectLuckyQueueListDesc();
        //判断是否已经达到最高等级
        if((luckyValueQueue==null)||(!luckyValueQueue.getLuckyQueueId().equals(luckyQueues.get(luckyQueues.size()-1).getId()))) {
            for (LuckyQueue luckyQueue : luckyQueues) {
                if (helpLuckyValue.getLuckyValue() > luckyQueue.getLuckyValue()) {
                    //如果用户所在队列为空  则必须是第一队列
                    if (luckyValueQueue == null && luckyQueue.getSort() == 1) {
                        //第一队列条件满足  提交晋级申请
                        return 1;
                    } else {
                        //除晋级第一队列外 其他队列晋级需要满足条件方可
                        if (luckyValueQueue != null) {
                            LuckyQueue luckyQueue1 = luckyQueueService.selectLuckyQueueById(luckyValueQueue.getLuckyQueueId());
                            //用户所在队列必须小于当前队列 才可晋级
                            if (luckyQueue1.getSort() < luckyQueue.getSort()) {
                                //判断邀请的好友是否满足条件  邀请的好友是否达到当前级别或者级别以上
                                Integer integer = luckyValueQueueService.selectUserLuckyQueueByInvitation(userByWxId.getRelationId(), luckyQueue.getSort());
                                //邀请的好友必须大于等于自己当前等级晋级所需人数
                                if (integer != null && integer >= luckyQueue1.getNumberOfPeople()) {
                                    //判断自己所在当前队列的位置
                                    Integer integer1 = luckyValueQueueService.selectLuckQueueLocationByUserId(userByWxId.getId(), luckyValueQueue.getLuckyQueueId());
                                    //当前队列总人数
                                    Integer integer2 = luckyValueQueueService.selectLuckQueueCount(luckyValueQueue.getLuckyQueueId());
                                    //队列晋级条件 前13名必须凑够13人方可晋级  后面按照9人一队为顺序 方可晋级
                                    if (integer1 != null && integer2 != null) {
                                        if (integer1 <= luckyQueue1.getStartPerson()) {
                                            //队列人数大于13人 提交晋级申请
                                            if (integer2 >= luckyQueue1.getStartPerson()) {
                                                //条件满足 提交晋级申请
                                                return 1;
                                            }
                                        } else {
                                            //根据当前所在位置 进行计算 必须满足9人方可晋级
                                            integer1 = integer - luckyQueue1.getStartPerson();
                                            integer2 = integer2 - luckyQueue1.getStartPerson();
                                            //根据当前位置和9的倍数 获取所在队列
                                            int i = integer1 / luckyQueue1.getIntervalPerson();
                                            //判断是否能够除尽
                                            int count;
                                            if (integer1 % luckyQueue1.getIntervalPerson() == 0) {
                                                count = i * luckyQueue1.getIntervalPerson();
                                            } else {
                                                count = (i * luckyQueue1.getIntervalPerson()) + luckyQueue1.getIntervalPerson();
                                            }
                                            //判断自己所在队列人数是否满足条件
                                            if (integer2 >= count) {
                                                //条件满足 提交晋级申请
                                                return 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    break;
                }
            }
        }
        return 0;
    }




    @Override
    public HelpGetTask selectById(Long id) {
        return helpGetTaskRepository.selectById(id);
    }

    @Override
    public Integer selectCount(Long id, Long userId) {
        return helpGetTaskRepository.selectCount(id,userId);
    }


}
